﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Bridge.Base.Constructs;
using Pathfinder.Base.Constructs;
using System.Collections.ObjectModel;
using Bridge.UI.Utilities;

namespace Pathfinder.Base.UI
{
    public class EnumInfoView<T> : ChangableObject
    {
        public EnumInfoView(T value, int range, ObservableCollection<UIEnumView<T>> possibleValues, PathfinderCharacterView pathfinderCharacterView)
        {
            if (value == null) throw new ArgumentNullException("value");
            if (!(value is Enum)) throw new ArgumentException("value is not an enum.", "value");

            mValue = value;
            mRange = range;
            PathfinderCharacterView = pathfinderCharacterView;
            PossibleValues = possibleValues;

            Enum enumValue = value as Enum;
            UIDisplayAttribute uiDisplayAttribute = enumValue.GetUIDisplayAttribute();
            if (uiDisplayAttribute != null && uiDisplayAttribute.Tag != null)
            {
                IsValued = (bool)uiDisplayAttribute.Tag;
            }
        }

        /// <summary>
        /// Current Value of the enum.
        /// </summary>
        public T Value
        {
            get { return mValue; }
            set
            {
                T oldValue = mValue;
                if (CheckPropertyChanged<T>("Value", ref mValue, ref value))
                {
                    NotifyPropertyChanged("UIEnumViewValue");
                    PathfinderCharacterView.Change<T>(oldValue, value, Range);
                    Enum enumValue = value as Enum;
                    UIDisplayAttribute uiDisplayAttribute = enumValue.GetUIDisplayAttribute();
                    if (uiDisplayAttribute != null && uiDisplayAttribute.Tag != null && uiDisplayAttribute.Tag is bool)
                    {
                        IsValued = (bool)uiDisplayAttribute.Tag;
                    }
                }
            }
        }

        public UIEnumView<T> UIEnumViewValue
        {
            get
            {
                foreach (var view in PossibleValues)
                {
                    if (view.Value.Equals(Value))
                    {
                        return view;
                    }
                }
                return null;
            }
            set
            {
                Value = value.Value;
            }
        }

        public int Range
        {
            get { return mRange;  }
            set
            {
                if (CheckPropertyChanged<int>("Range", ref mRange, ref value))
                {
                    NotifyPropertyChanged("StringRange");
                    PathfinderCharacterView.Set<T>(Value, value);
                }
            }
        }

        public string StringRange
        {
            get { return mRange.ToString(); }
            set
            {
                int possibleValue = -1;
                if (int.TryParse(value, out possibleValue))
                {
                    IsValid = true;
                    Range = possibleValue;
                }
                else
                {
                    IsValid = false;
                }
            }
        }

        public bool IsValid
        {
            get { return mIsValid; }
            set { CheckPropertyChanged<bool>("IsValid", ref mIsValid, ref value); }
        }

        public bool IsValued
        {
            get { return mIsValued; }
            set { CheckPropertyChanged<bool>("IsValued", ref mIsValued, ref value); }
        }

        public ObservableCollection<UIEnumView<T>> PossibleValues
        {
            get { return mPossibleValues; }
            set
            {
                if (CheckPropertyChanged<ObservableCollection<UIEnumView<T>>>("PossibleValues", ref mPossibleValues, ref value))
                {
                    NotifyPropertyChanged("UIEnumViewValue");
                }
            }
        }

        public PathfinderCharacterView PathfinderCharacterView
        {
            get { return mPathfinderCharacterView; }
            set { CheckPropertyChanged<PathfinderCharacterView>("PathfinderCharacterView", ref mPathfinderCharacterView, ref value); }
        }

        private T mValue;
        private int mRange;
        private bool mIsValued = true;
        private bool mIsValid = true;
        private ObservableCollection<UIEnumView<T>> mPossibleValues;
        private PathfinderCharacterView mPathfinderCharacterView;
    }
}
